//
//  NewspaperShelViewController.m
//  JianChaRiBao
//
//  Created by yuzhou on 13-11-4.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "NewspaperShelViewController.h"
#import "DownloadArticle.h"
#import "NewspaperCell.h"

@interface NewspaperShelViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    IBOutlet UITableView *newspaperTableView;
}
@property (nonatomic,retain) NSArray *newspaperArray;
@end

@implementation NewspaperShelViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDisplay) name:@"didDownloadNewspaper" object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self updateDisplay];
}

- (void)updateDisplay
{
    self.newspaperArray = [[DownloadArticle shareDownloadArticle] getDownloadedNewspaper];
    [newspaperTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _newspaperArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentify = @"NewspaperCell";
    NewspaperCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentify];
    if (cell == nil) {
        cell = [[[NewspaperCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentify] autorelease];
    }
    NSLog(@"dict:%@",[_newspaperArray objectAtIndex:indexPath.row]);
    [cell setCategory:[_newspaperArray objectAtIndex:indexPath.row] title:[[_newspaperArray objectAtIndex:indexPath.row] objectForKey:@"pub_date"]];

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 155;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [newspaperTableView release];
    [self.newspaperArray release];
    [super dealloc];
}
- (void)viewDidUnload {
    [newspaperTableView release];
    newspaperTableView = nil;
    [super viewDidUnload];
}
@end
